﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using cecbData;
using cecbClasses.Data_Adapters;

namespace CECB.Member
{
    /// <summary>
    /// Interaction logic for V_MemberSearch_Window.xaml
    /// </summary>
    public partial class V_MemberSearch_Window : Window
    {
        public V_MemberSearch_Window()
        {
            InitializeComponent();
            MemberAutocompleteTextBox.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getAllMembers();
        }

        Members memberObj;
        Boolean isMemberSelected = false;
        private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9]+");
            e.Handled = regex.IsMatch(e.Text);

        }


        private void Email_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {

        }

        private void button_Save_Cancel_Click(object sender, RoutedEventArgs e)
        {

        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void NewButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void listBoxContacts_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBoxContacts.Items.Count > 0)
            {
                try
                {
                    MemberContacts memContactObj = (MemberContacts)listBoxContacts.SelectedItem;
                    textBoxUpdateContact.Text = memContactObj.mem_contactNo.ToString();
                    _oldContactNo = memContactObj.mem_contactNo.ToString();
                    _memID = memContactObj.mem_reference.ToString();
                    label_Change.Content = "Change Contact No";
                }
                catch (Exception)
                {


                }
            }
        }

        private void ReferenceTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((AutoCompleteBox)sender).SelectedItem != null)
            {

                isMemberSelected = true;
                memberObj = (Members)((AutoCompleteBox)sender).SelectedItem;

                cecbClasses.Data_Adapters.Member.Func_Members memInfo = new cecbClasses.Data_Adapters.Member.Func_Members();

                listBoxContacts.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getMemberContatcs(memberObj.mem_reference);
                listBoxEmail.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getMemberEmail(memberObj.mem_reference);
                textBoxUpdateContact.Clear();

            }
        }




        String _oldContactNo = "";
        String _memID = "";
        String _oldEmail = "";
        String _memIDEmail = "";


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (textBoxUpdateContact.Text == "")
            {
                MessageBox.Show("Nothing selected to modify", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (textBoxUpdateContact.Text != "" && MemberAutocompleteTextBox.Text == "")
            {
                MessageBox.Show("Select the Member First", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                if (label_Change.Content == "Change Contact No")
                {
                    if (textBoxUpdateContact.Text.Length != 0 && textBoxUpdateContact.Text.Length != 10)
                    {
                        MessageBox.Show("Enter a valid contact No ", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }

                    else
                    {
                        if (new cecbClasses.Data_Adapters.Member.Func_Members().updateMemberContact(textBoxUpdateContact.Text.Trim(), _memID, _oldContactNo))
                        {
                            MessageBox.Show("Contact Updated Successfully!", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                            textBoxUpdateContact.Clear();
                            listBoxContacts.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getMemberContatcs(MemberAutocompleteTextBox.Text);



                        }
                    }
                }
                else
                {
                    if (textBoxUpdateContact.Text.Length != 0 && (!Regex.IsMatch(textBoxUpdateContact.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")))
                    {
                        MessageBox.Show("Enter a valid Email Address", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        if (new cecbClasses.Data_Adapters.Member.Func_Members().updateMemberEmails(textBoxUpdateContact.Text.Trim(), _memIDEmail, _oldEmail))
                        {
                            MessageBox.Show("Email Address Updated Successfully!", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                            textBoxUpdateContact.Clear();

                            listBoxContacts.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getMemberEmail(MemberAutocompleteTextBox.Text);
                        }
                    }
                }
            }
            listBoxContacts.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getMemberContatcs(MemberAutocompleteTextBox.Text);
            listBoxEmail.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getMemberEmail(MemberAutocompleteTextBox.Text);
        }


        private void listBoxEmail_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBoxEmail.Items.Count > 0)
            {
                try
                {
                    MemberEmails memEmail = (MemberEmails)listBoxEmail.SelectedItem;
                    textBoxUpdateContact.Text = memEmail.mem_email.ToString();
                    _oldEmail = memEmail.mem_email.ToString();
                    _memIDEmail = memEmail.mem_reference.ToString();
                    label_Change.Content = "Change Email Address";

                }
                catch (Exception)
                {


                }
            }
        }

        private void textBox_ContactNo3_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9]+");
            e.Handled = regex.IsMatch(e.Text);


        }

        private void textBox_ContactNo3_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void textBoxUpdateContact_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (label_Change.Content == "Change Contact No")
            {
                Regex regex = new Regex("[^0-9]+");
                e.Handled = regex.IsMatch(e.Text);
            }
        }

        private void Label_SourceUpdated(object sender, DataTransferEventArgs e)
        {

        }




        private void textBox_Modifyusername_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void textBox_Modifyusername_SelectionChanged(object sender, RoutedEventArgs e)
        {

        }

        private void textBox_Modifyusername_TextInput(object sender, TextCompositionEventArgs e)
        {

        }

        private void textBox_Modifyusername_IsHitTestVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

        }

        private void comboBox_Specilised_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void textBoxUpdateContact_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }


    }
}
